#include "Enemy.h"

#include "Utils.h"
#include <Windows.h>
#include <gl/GL.h>
#include <SDL.h>

#include "Audio.h"

Enemy::Enemy(const Position& p) : Quad(p)
{
	m_width = 10;
	m_height = 10;
	m_intersecting = true;
	m_dead = false;

	if(rand()%2 == 0)
		m_texture.setFilename("textures/koopa.png");
	else
		m_texture.setFilename("textures/goomba.png");

	m_firstFall = true;
	m_texture.setMirror(true);
}

void Enemy::update( float frametime )
{
	if(m_dead){
		return;
	}
	
	// Movement
	m_speed.x -= 2;
	
	// Friction
	m_speed.x *= 0.95;

	// Max speed
	m_speed.x = (m_speed.x > 0 ? 1 : -1 ) * MIN( abs(m_speed.x), 25 );
	m_speed.y = (m_speed.y > 0 ? 1 : -1 ) * MIN( abs(m_speed.y), 25 * (m_firstFall ? 10 : 1 ) );

	// TODO: gravity
	if(!m_intersecting)
		m_speed.y -= 10;

	m_pos.x += m_speed.x * frametime;
	m_pos.y += m_speed.y * frametime;

	m_intersecting = false;

	//als naar beneden valt	
	if(m_pos.y < -600 && !m_dead){
		m_dead = true;
		Delete();
	}
}

void Enemy::setIntersect( Entity * h, int side )
{

	if(side == 1|| side == 2){
		//sario heeft op dit object gebotst.
		m_speed.y = 0;
		m_intersecting = true;
		m_firstFall = false;
	}
}

void Enemy::setDead(){

	if( !m_dead )
	{
		int r = rand() % 3;

		switch( r )
		{
		case 0:
			Audio::getInstance().play("music/mario_1.ogg");
			break;

		case 1:
			Audio::getInstance().play("music/mario_2.ogg");
			break;

		case 2:
			Audio::getInstance().play("music/mario_3.ogg");
			break;
		}
	}

	m_dead = true;
	m_height /= 2;

	if( m_height < 5 )
		Delete();
}

bool Enemy::isDead(){

	return m_dead;
}